package com.xjscrm.console.service.common.seat.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import com.xjscrm.common.constant.ResponseMsg;
import com.xjscrm.common.exception.BusinessException;
import com.xjscrm.common.param.seat.SeatBuyParam;
import com.xjscrm.common.param.seat.SeatOptParam;
import com.xjscrm.common.param.seat.SeatSearchParam;
import com.xjscrm.common.result.ResponseCode;
import com.xjscrm.common.vo.seat.SeatBuyVo;
import com.xjscrm.common.vo.seat.SeatPriceVO;
import com.xjscrm.common.vo.seat.SeatVo;
import com.xjscrm.console.mapper.SeatMapper;
import com.xjscrm.console.param.BaseDto;
import com.xjscrm.console.service.common.seat.SeatService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class SeatServiceImpl implements SeatService {

    @Autowired
    private SeatMapper seatMapper;

    @Autowired
    private RestTemplate restTemplate;

    @Value("${auth.url}")
    private String authUrl;
    @Override
    public Object totalSeatNum(BaseDto baseDto) {
        if(baseDto.getMerId()==null){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
        }
       Integer totalSeatNum= seatMapper.totalSeatNum(baseDto.getMerId());
        return totalSeatNum;
    }

    @Override
    public Page<SeatVo> getSeat(SeatSearchParam searchParam) {
      if(searchParam.getMerId()==null){
          throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.MERID_NULL);
      }
        Page<SeatVo> page=new Page<>();
        String httpRequstUrl=authUrl+"/seat/search";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<SeatSearchParam> objectHttpEntity = new HttpEntity<SeatSearchParam>(searchParam,headers);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(httpRequstUrl, objectHttpEntity, JSONObject.class);
        if (response != null && response.getStatusCode() == HttpStatus.OK) {
            JSONObject result = response.getBody();
            if(result != null && result.getInteger("code").intValue() == 200){//请求成功
                page = JSONObject.toJavaObject(result.getJSONObject("data"), Page.class);
            }else{
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, result.get("msg")!=null? result.getString("msg"):"调用席位查询接口失败");
            }
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
        }
        return page;
    }


    @Override
    public Object delete(SeatOptParam opt) {
        if (Objects.isNull(opt) || ObjectUtils.isEmpty(opt.getIds())) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        Boolean data=false;
        String httpRequstUrl=authUrl+"/seat/delete";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<SeatOptParam> objectHttpEntity = new HttpEntity<SeatOptParam>(opt,headers);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(httpRequstUrl, objectHttpEntity, JSONObject.class);
        if (response != null && response.getStatusCode() == HttpStatus.OK) {
            JSONObject result = response.getBody();
            if(result != null && result.getInteger("code").intValue() == 200){//请求成功
                 data = JSONObject.toJavaObject(result.getJSONObject("data"), Boolean.class);
            }else{
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, result.get("msg")!=null? result.getString("msg"):"调用席位查询接口失败");
            }
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
        }
        return data;
    }

    @Override
    public Object renewal(SeatBuyParam opt) {
        if (Objects.isNull(opt) || ObjectUtils.isEmpty(opt.getIds()) || Objects.isNull(opt.getMerId())) {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.PARAMETER_ERROR);
        }
        SeatBuyVo seatBuyVo=new SeatBuyVo();
        String httpRequstUrl=authUrl+"/seat/renewal";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<SeatBuyParam> objectHttpEntity = new HttpEntity<SeatBuyParam>(opt,headers);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(httpRequstUrl, objectHttpEntity, JSONObject.class);
        if (response != null && response.getStatusCode() == HttpStatus.OK) {
            JSONObject result = response.getBody();
            //请求成功
            if(result != null && result.getInteger("code").intValue() == 200)
                seatBuyVo = JSONObject.toJavaObject(result.getJSONObject("data"), SeatBuyVo.class);
            else{
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, result.get("msg")!=null? result.getString("msg"):"调用席位查询接口失败");
            }
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
        }
        return seatBuyVo;
    }

    @Override
    public Object buy(SeatBuyParam opt) {
        if (Objects.isNull(opt) || Objects.isNull(opt.getMerId())) {
            throw new BusinessException("购买席位参数异常");
        }
        SeatBuyVo seatBuyVo=new SeatBuyVo();
        String httpRequstUrl=authUrl+"/seat/buy";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<SeatBuyParam> objectHttpEntity = new HttpEntity<SeatBuyParam>(opt,headers);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(httpRequstUrl, objectHttpEntity, JSONObject.class);
        if (response != null && response.getStatusCode() == HttpStatus.OK) {
            JSONObject result = response.getBody();
            //请求成功
            if(result != null && result.getInteger("code").intValue() == 200)
                seatBuyVo = JSONObject.toJavaObject(result.getJSONObject("data"), SeatBuyVo.class);
            else{
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, result.get("msg")!=null? result.getString("msg"):"调用席位查询接口失败");
            }
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
        }
        return seatBuyVo;
    }

    @Override
    public Object buySeatConfirm(SeatOptParam opt) {
        if (Objects.isNull(opt) || ObjectUtils.isEmpty(opt.getMerId()) || StringUtils.isBlank(opt.getTradeCode())) {
            throw new BusinessException("购买席位参数异常");
        }
        Boolean data=false;
        String httpRequstUrl=authUrl+"/seat/buy/confirm";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<SeatOptParam> objectHttpEntity = new HttpEntity<SeatOptParam>(opt,headers);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(httpRequstUrl, objectHttpEntity, JSONObject.class);
        if (response != null && response.getStatusCode() == HttpStatus.OK) {
            JSONObject result = response.getBody();
            if(result != null && result.getInteger("code").intValue() == 200){//请求成功
                 data = JSONObject.toJavaObject(result.getJSONObject("data"), Boolean.class);
            }else{
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, result.get("msg")!=null? result.getString("msg"):"调用席位查询接口失败");
            }
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
        }
        return data;
    }


    @Override
    public  List<SeatPriceVO> buySeatPrice() {
        List<SeatPriceVO> seatPriceVOList=new ArrayList<>();
        String httpRequstUrl=authUrl+"/seat/buy/price";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        ResponseEntity<JSONObject> response = restTemplate.postForEntity(httpRequstUrl,null, JSONObject.class);
        if (response != null && response.getStatusCode() == HttpStatus.OK) {
            JSONObject result = response.getBody();
            if(result != null && result.getInteger("code").intValue() == 200){//请求成功
                JSONArray data1 = result.getJSONArray("data");
               seatPriceVOList=data1.toJavaList(SeatPriceVO.class);
            }else{
                throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, result.get("msg")!=null? result.getString("msg"):"调用席位查询接口失败");
            }
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, ResponseMsg.NET_ERROR);
        }
        return seatPriceVOList;
    }
}
